package com.zhengxing.jienci.content.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhengxing.jienci.common.constant.RedisKey;
import com.zhengxing.jienci.common.pojo.Comment;
import com.zhengxing.jienci.common.pojo.CustomizePage;
import com.zhengxing.jienci.common.pojo.SysUser;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.common.util.CommonUtils;
import com.zhengxing.jienci.common.util.DateUtil;
import com.zhengxing.jienci.common.util.IdGenerateUtil;
import com.zhengxing.jienci.content.mapper.CommentMapper;
import com.zhengxing.jienci.content.service.CommentService;
import com.zhengxing.jienci.feign.UserFeginApi;
import com.zhengxing.jienci.web.context.SecurityContext;

@Service
public class CommentServiceImpl implements CommentService{
	
	@Autowired
	private CommentMapper commentMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Autowired
	private UserFeginApi userFeginApi;
	
	@Override
	public SimpleResponse save(Comment comment) {
		//评论信息的添加
		String userId = SecurityContext.getUserId();
		comment.setId(IdGenerateUtil.getId());
		comment.setFromUserId(userId);
		comment.setDateTime(new Date());
		int result = commentMapper.insert(comment);
		//总回复数的添加
		redisTemplate.opsForValue().increment(RedisKey.SUM_BACK_OPERATION_PREFIX + comment.getInformationId());
		if(StringUtils.isBlank(comment.getGrandParentId()) && StringUtils.isBlank(comment.getParentId())) {
			//文章回复数的添加
			redisTemplate.opsForValue().increment(RedisKey.INFORMATION_BACK_OPERATION_PREFIX + comment.getInformationId());
		} else if(StringUtils.isNotBlank(comment.getGrandParentId()) && StringUtils.isBlank(comment.getParentId())){
			redisTemplate.opsForValue().increment(RedisKey.COMMENT_BACK_OPERATION_PREFIX + comment.getGrandParentId());
		} else {
			redisTemplate.opsForValue().increment(RedisKey.COMMENT_BACK_OPERATION_PREFIX + comment.getParentId());
		}
		return CommonUtils.msg(result);
	}

	@Override
	public Page<Comment> listForPage(CustomizePage page, Comment comment) {
		String userId = SecurityContext.getUserId();
		Page<Comment> pages = new Page<Comment>(page.getPageNow(), page.getPageSize());
		List<Comment> comments = commentMapper.listForPage(pages, comment);
		comments.stream().forEach(comm -> {
			String commentId = comm.getId();
			//评论时间的转换
			String showDateTime = DateUtil.showDateTime(comm.getDateTime(), new Date());
			comm.setShowDateTime(showDateTime);
			//获取点赞数
			Long goodNumber = redisTemplate.opsForSet().size(RedisKey.COMMENT_IS_GOOD_OPERATION_PRFIX + commentId);
			comm.setGoodNumber(goodNumber);
			//获取是否点赞
			Boolean isGood = redisTemplate.opsForSet().isMember(RedisKey.COMMENT_IS_GOOD_OPERATION_PRFIX + commentId, userId);
			comm.setGood(isGood);
			//获取回复数
			String backNumber = redisTemplate.opsForValue().get(RedisKey.COMMENT_BACK_OPERATION_PREFIX + commentId);
			comm.setBackNumber(backNumber==null?0:Long.parseLong(backNumber));
			SysUser fromUser = userFeginApi.getSimpleUserById(comm.getFromUserId());
			comm.setFromNickname(fromUser.getNickname());
			comm.setFromUserImage(fromUser.getImage());
			if(StringUtils.isNotBlank(comm.getToUserId())) {
				SysUser toUser = userFeginApi.getSimpleUserById(comm.getFromUserId());
				comm.setToNickname(toUser.getNickname());
			}
		});
		pages.setRecords(comments);
		return pages;
	}

	@Override
	public SimpleResponse giveOrCancelGood(String id) {
		Long result = 0L;
		String userId = SecurityContext.getUserId();
		String key = RedisKey.COMMENT_IS_GOOD_OPERATION_PRFIX + id;
		if(!redisTemplate.opsForSet().isMember(key, userId)) {
			result = redisTemplate.opsForSet().add(key, userId);
		} else {
			result = redisTemplate.opsForSet().remove(key, userId);
		}
		return CommonUtils.msg(result);
	}

	@Override
	public SimpleResponse isGood(String id) {
		String userId = SecurityContext.getUserId();
		Boolean isGood = redisTemplate.opsForSet().isMember(RedisKey.COMMENT_IS_GOOD_OPERATION_PRFIX + id, userId);
		return SimpleResponse.success().put("isGood", isGood);
	}
	
	

}
